package cn.yunhe.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunhe.model.User;
import cn.yunhe.service.UserService;
import cn.yunhe.util.PageUtil;

@WebServlet("/MainServlet")
public class MainServlet extends HttpServlet{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String currPage=req.getParameter("pageIndex");
		if(currPage==null) {
			currPage="1";
		}
		int pageIndex=Integer.parseInt(currPage);
		UserService userSe=new UserService();
		int count=userSe.getCount();
		PageUtil pu=new PageUtil();
		int totalPage=pu.getTotalPage(count, PageUtil.pageSize);
		if(pageIndex>totalPage) {
			pageIndex=totalPage;
		}
		if(pageIndex<1) {
			pageIndex=1;
		}
		List<User> list=userSe.getAllByPage(pageIndex, PageUtil.pageSize);
		req.setAttribute("list", list);
		req.setAttribute("pageIndex", pageIndex);
		req.setAttribute("totalPage", totalPage);
		
		req.getRequestDispatcher("main.jsp").forward(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
